/*file chiron src/test/http/url.js */
/*preamble

    Copyright (c) 2002-2008 Kris Kowal <http://cixar.com/~kris.kowal>
    MIT License
    
    The license terms are stated in full in <license.rst> and at the end
    of all source files.

*/
/**
    test cases for URI relation and resolution.
    cases for parsing and formatting are in ``test/http/formatUrl.js``.
    these cases were borrowed from ``test_uri.py`` from 
    http://cvs.4suite.org/viewcvs/4Suite/test/Lib/test_uri.py?view=markup
*/

include('base');
include('http');
include('urls');
include('test');
include('printf');

var tests = [
];

// Test cases for Absolutize() ================================================
var bases = [
    'http://a/b/c/d;p?q',
    'http://a/b/c/d;p?q=1/2',
    'http://a/b/c/d;p=1/2?q',
    'fred:///s//a/b/c',
    'http:///s//a/b/c'
];

//[ref, base, expected)
var resolveTests = [
    // http://lists.w3.org/Archives/Public/uri/2004Feb/0114.html
   ['../c',  'foo:a/b', 'foo:c'],
   ['foo:.', 'foo:a',   'foo:'],
   ['/foo/../../../bar', 'zz:abc', 'zz:/bar'],
   ['/foo/../bar',       'zz:abc', 'zz:/bar'],
   ['foo/../../../bar',  'zz:abc', 'zz:bar'],
   ['foo/../bar',        'zz:abc', 'zz:bar'],
   ['zz:.',              'zz:abc', 'zz:'],
   ['/.'      , bases[0], 'http://a/'],
   ['/.foo'   , bases[0], 'http://a/.foo'],
   ['.foo'    , bases[0], 'http://a/b/c/.foo'],

    // http://gbiv.com/protocols/uri/test/rel_examples1.html
    // examples from RFC 2396
   ['g:h'     , bases[0], 'g:h'],
   ['g'       , bases[0], 'http://a/b/c/g'],
   ['./g'     , bases[0], 'http://a/b/c/g'],
   ['g/'      , bases[0], 'http://a/b/c/g/'],
   ['/g'      , bases[0], 'http://a/g'],
   ['//g'     , bases[0], 'http://g'],
    // changed with RFC 2396bis
    //('?y'      , bases[0], 'http://a/b/c/d;p?y'],
   ['?y'      , bases[0], 'http://a/b/c/d;p?y'],
   ['g?y'     , bases[0], 'http://a/b/c/g?y'],
    // changed with RFC 2396bis
    //('#s'      , bases[0], CURRENT_DOC_URI + '#s'],
   ['#s'      , bases[0], 'http://a/b/c/d;p?q#s'],
   ['g#s'     , bases[0], 'http://a/b/c/g#s'],
   ['g?y#s'   , bases[0], 'http://a/b/c/g?y#s'],
   [';x'      , bases[0], 'http://a/b/c/;x'],
   ['g;x'     , bases[0], 'http://a/b/c/g;x'],
   ['g;x?y#s' , bases[0], 'http://a/b/c/g;x?y#s'],
    // changed with RFC 2396bis
    //(''        , bases[0], CURRENT_DOC_URI],
   [''        , bases[0], 'http://a/b/c/d;p?q'],
   ['.'       , bases[0], 'http://a/b/c/'],
   ['./'      , bases[0], 'http://a/b/c/'],
   ['..'      , bases[0], 'http://a/b/'],
   ['../'     , bases[0], 'http://a/b/'],
   ['../g'    , bases[0], 'http://a/b/g'],
   ['../..'   , bases[0], 'http://a/'],
   ['../../'  , bases[0], 'http://a/'],
   ['../../g' , bases[0], 'http://a/g'],
   ['../../../g', bases[0], ('http://a/../g', 'http://a/g')],
   ['../../../../g', bases[0], ('http://a/../../g', 'http://a/g')],
    // changed with RFC 2396bis
    //('/./g', bases[0], 'http://a/./g'],
   ['/./g', bases[0], 'http://a/g'],
    // changed with RFC 2396bis
    //('/../g', bases[0], 'http://a/../g'],
   ['/../g', bases[0], 'http://a/g'],
   ['g.', bases[0], 'http://a/b/c/g.'],
   ['.g', bases[0], 'http://a/b/c/.g'],
   ['g..', bases[0], 'http://a/b/c/g..'],
   ['..g', bases[0], 'http://a/b/c/..g'],
   ['./../g', bases[0], 'http://a/b/g'],
   ['./g/.', bases[0], 'http://a/b/c/g/'],
   ['g/./h', bases[0], 'http://a/b/c/g/h'],
   ['g/../h', bases[0], 'http://a/b/c/h'],
   ['g;x=1/./y', bases[0], 'http://a/b/c/g;x=1/y'],
   ['g;x=1/../y', bases[0], 'http://a/b/c/y'],
   ['g?y/./x', bases[0], 'http://a/b/c/g?y/./x'],
   ['g?y/../x', bases[0], 'http://a/b/c/g?y/../x'],
   ['g#s/./x', bases[0], 'http://a/b/c/g#s/./x'],
   ['g#s/../x', bases[0], 'http://a/b/c/g#s/../x'],
   ['http:g', bases[0], ('http:g', 'http://a/b/c/g')],
   ['http:', bases[0], ('http:', bases[0])],
    // not sure where this one originated
   ['/a/b/c/./../../g', bases[0], 'http://a/a/g'],

    // http://gbiv.com/protocols/uri/test/rel_examples2.html
    // slashes in base URI's query args
   ['g'       , bases[1], 'http://a/b/c/g'],
   ['./g'     , bases[1], 'http://a/b/c/g'],
   ['g/'      , bases[1], 'http://a/b/c/g/'],
   ['/g'      , bases[1], 'http://a/g'],
   ['//g'     , bases[1], 'http://g'],
    // changed in RFC 2396bis
   //('?y'      , bases[1], 'http://a/b/c/?y'],
   ['?y'      , bases[1], 'http://a/b/c/d;p?y'],
   ['g?y'     , bases[1], 'http://a/b/c/g?y'],
   ['g?y/./x' , bases[1], 'http://a/b/c/g?y/./x'],
   ['g?y/../x', bases[1], 'http://a/b/c/g?y/../x'],
   ['g#s'     , bases[1], 'http://a/b/c/g#s'],
   ['g#s/./x' , bases[1], 'http://a/b/c/g#s/./x'],
   ['g#s/../x', bases[1], 'http://a/b/c/g#s/../x'],
   ['./'      , bases[1], 'http://a/b/c/'],
   ['../'     , bases[1], 'http://a/b/'],
   ['../g'    , bases[1], 'http://a/b/g'],
   ['../../'  , bases[1], 'http://a/'],
   ['../../g' , bases[1], 'http://a/g'],

    // http://gbiv.com/protocols/uri/test/rel_examples3.html
    // slashes in path params
    // all of these changed in RFC 2396bis
   ['g'       , bases[2], 'http://a/b/c/d;p=1/g'],
   ['./g'     , bases[2], 'http://a/b/c/d;p=1/g'],
   ['g/'      , bases[2], 'http://a/b/c/d;p=1/g/'],
   ['g?y'     , bases[2], 'http://a/b/c/d;p=1/g?y'],
   [';x'      , bases[2], 'http://a/b/c/d;p=1/;x'],
   ['g;x'     , bases[2], 'http://a/b/c/d;p=1/g;x'],
   ['g;x=1/./y', bases[2], 'http://a/b/c/d;p=1/g;x=1/y'],
   ['g;x=1/../y', bases[2], 'http://a/b/c/d;p=1/y'],
   ['./'      , bases[2], 'http://a/b/c/d;p=1/'],
   ['../'     , bases[2], 'http://a/b/c/'],
   ['../g'    , bases[2], 'http://a/b/c/g'],
   ['../../'  , bases[2], 'http://a/b/'],
   ['../../g' , bases[2], 'http://a/b/g'],

    // http://gbiv.com/protocols/uri/test/rel_examples4.html
    // double and triple slash, unknown scheme
   ['g:h'     , bases[3], 'g:h'],
   ['g'       , bases[3], 'fred:///s//a/b/g'],
   ['./g'     , bases[3], 'fred:///s//a/b/g'],
   ['g/'      , bases[3], 'fred:///s//a/b/g/'],
   ['/g'      , bases[3], 'fred:///g'],  // may change to fred:///s//a/g
   ['//g'     , bases[3], 'fred://g'],   // may change to fred:///s//g
   ['//g/x'   , bases[3], 'fred://g/x'], // may change to fred:///s//g/x
   ['///g'    , bases[3], 'fred:///g'],
   ['./'      , bases[3], 'fred:///s//a/b/'],
   ['../'     , bases[3], 'fred:///s//a/'],
   ['../g'    , bases[3], 'fred:///s//a/g'],
   ['../../'  , bases[3], 'fred:///s//'],    // may change to fred:///s//a/../
   ['../../g' , bases[3], 'fred:///s//g'],   // may change to fred:///s//a/../g
   ['../../../g', bases[3], 'fred:///s/g'],  // may change to fred:///s//a/../../g
   ['../../../../g', bases[3], 'fred:///g'], // may change to fred:///s//a/../../../g

    // http://gbiv.com/protocols/uri/test/rel_examples5.html
    // double and triple slash, well-known scheme
   ['g:h'     , bases[4], 'g:h'],
   ['g'       , bases[4], 'http:///s//a/b/g'],
   ['./g'     , bases[4], 'http:///s//a/b/g'],
   ['g/'      , bases[4], 'http:///s//a/b/g/'],
   ['/g'      , bases[4], 'http:///g'],  // may change to http:///s//a/g
   ['//g'     , bases[4], 'http://g'],   // may change to http:///s//g
   ['//g/x'   , bases[4], 'http://g/x'], // may change to http:///s//g/x
   ['///g'    , bases[4], 'http:///g'],
   ['./'      , bases[4], 'http:///s//a/b/'],
   ['../'     , bases[4], 'http:///s//a/'],
   ['../g'    , bases[4], 'http:///s//a/g'],
   ['../../'  , bases[4], 'http:///s//'],    // may change to http:///s//a/../
   ['../../g' , bases[4], 'http:///s//g'],   // may change to http:///s//a/../g
   ['../../../g', bases[4], 'http:///s/g'],  // may change to http:///s//a/../../g
   ['../../../../g', bases[4], 'http:///g'], // may change to http:///s//a/../../../g

    // from Dan Connelly's tests in http://www.w3.org/2000/10/swap/uripath.py
   ["bar:abc", "foo:xyz", "bar:abc"],
   ['../abc', 'http://example/x/y/z', 'http://example/x/abc'],
   ['http://example/x/abc', 'http://example2/x/y/z', 'http://example/x/abc'],
   ['../r', 'http://ex/x/y/z', 'http://ex/x/r'],
   ['q/r', 'http://ex/x/y', 'http://ex/x/q/r'],
   ['q/r#s', 'http://ex/x/y', 'http://ex/x/q/r#s'],
   ['q/r#s/t', 'http://ex/x/y', 'http://ex/x/q/r#s/t'],
   ['ftp://ex/x/q/r', 'http://ex/x/y', 'ftp://ex/x/q/r'],
   ['', 'http://ex/x/y', 'http://ex/x/y'],
   ['', 'http://ex/x/y/', 'http://ex/x/y/'],
   ['', 'http://ex/x/y/pdq', 'http://ex/x/y/pdq'],
   ['z/', 'http://ex/x/y/', 'http://ex/x/y/z/'],
   ['#Animal', 'file:/swap/test/animal.rdf', 'file:/swap/test/animal.rdf#Animal'],
   ['../abc', 'file:/e/x/y/z', 'file:/e/x/abc'],
   ['/example/x/abc', 'file:/example2/x/y/z', 'file:/example/x/abc'],
   ['../r', 'file:/ex/x/y/z', 'file:/ex/x/r'],
   ['/r', 'file:/ex/x/y/z', 'file:/r'],
   ['q/r', 'file:/ex/x/y', 'file:/ex/x/q/r'],
   ['q/r#s', 'file:/ex/x/y', 'file:/ex/x/q/r#s'],
   ['q/r#', 'file:/ex/x/y', 'file:/ex/x/q/r#'],
   ['q/r#s/t', 'file:/ex/x/y', 'file:/ex/x/q/r#s/t'],
   ['ftp://ex/x/q/r', 'file:/ex/x/y', 'ftp://ex/x/q/r'],
   ['', 'file:/ex/x/y', 'file:/ex/x/y'],
   ['', 'file:/ex/x/y/', 'file:/ex/x/y/'],
   ['', 'file:/ex/x/y/pdq', 'file:/ex/x/y/pdq'],
   ['z/', 'file:/ex/x/y/', 'file:/ex/x/y/z/'],
   ['file://meetings.example.com/cal#m1', 'file:/devel/WWW/2000/10/swap/test/reluri-1.n3', 'file://meetings.example.com/cal#m1'],
   ['file://meetings.example.com/cal#m1', 'file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3', 'file://meetings.example.com/cal#m1'],
   ['./#blort', 'file:/some/dir/foo', 'file:/some/dir/#blort'],
   ['./#', 'file:/some/dir/foo', 'file:/some/dir/#'],
    // Ryan Lee
   ["./", "http://example/x/abc.efg", "http://example/x/"],

    // Graham Klyne's tests
    // http://www.ninebynine.org/Software/HaskellUtils/Network/UriTest.xls
    // 01-31 are from Connelly's cases

    // 32-49
   ['./q:r', 'http://ex/x/y', 'http://ex/x/q:r'],
   ['./p=q:r', 'http://ex/x/y', 'http://ex/x/p=q:r'],
   ['?pp/rr', 'http://ex/x/y?pp/qq', 'http://ex/x/y?pp/rr'],
   ['y/z', 'http://ex/x/y?pp/qq', 'http://ex/x/y/z'],
   ['local/qual@domain.org#frag', 'mailto:local', 'mailto:local/qual@domain.org#frag'],
   ['more/qual2@domain2.org#frag', 'mailto:local/qual1@domain1.org', 'mailto:local/more/qual2@domain2.org#frag'],
   ['y?q', 'http://ex/x/y?q', 'http://ex/x/y?q'],
   ['/x/y?q', 'http://ex?p', 'http://ex/x/y?q'],
   ['c/d',  'foo:a/b', 'foo:a/c/d'],
   ['/c/d', 'foo:a/b', 'foo:/c/d'],
   ['', 'foo:a/b?c#d', 'foo:a/b?c'],
   ['b/c', 'foo:a', 'foo:b/c'],
   ['../b/c', 'foo:/a/y/z', 'foo:/a/b/c'],
   ['./b/c', 'foo:a', 'foo:b/c'],
   ['/./b/c', 'foo:a', 'foo:/b/c'],
   ['../../d', 'foo://a//b/c', 'foo://a/d'],
   ['.', 'foo:a', 'foo:'],
   ['..', 'foo:a', 'foo:'],

    // 50-57[cf. TimBL comments --
    //  http://lists.w3.org/Archives/Public/uri/2003Feb/0028.html,
    //  http://lists.w3.org/Archives/Public/uri/2003Jan/0008.html)
   ['abc', 'http://example/x/y%2Fz', 'http://example/x/abc'],
   ['../../x%2Fabc', 'http://example/a/x/y/z', 'http://example/a/x%2Fabc'],
   ['../x%2Fabc', 'http://example/a/x/y%2Fz', 'http://example/a/x%2Fabc'],
   ['abc', 'http://example/x%2Fy/z', 'http://example/x%2Fy/abc'],
   ['q%3Ar', 'http://ex/x/y', 'http://ex/x/q%3Ar'],
   ['/x%2Fabc', 'http://example/x/y%2Fz', 'http://example/x%2Fabc'],
   ['/x%2Fabc', 'http://example/x/y/z', 'http://example/x%2Fabc'],
   ['/x%2Fabc', 'http://example/x/y%2Fz', 'http://example/x%2Fabc'],

    // 70-77
   ['local2@domain2', 'mailto:local1@domain1?query1', 'mailto:local2@domain2'],
   ['local2@domain2?query2', 'mailto:local1@domain1', 'mailto:local2@domain2?query2'],
   ['local2@domain2?query2', 'mailto:local1@domain1?query1', 'mailto:local2@domain2?query2'],
   ['?query2', 'mailto:local@domain?query1', 'mailto:local@domain?query2'],
   ['local@domain?query2', 'mailto:?query1', 'mailto:local@domain?query2'],
   ['?query2', 'mailto:local@domain?query1', 'mailto:local@domain?query2'],
   ['http://example/a/b?c/../d', 'foo:bar', 'http://example/a/b?c/../d'],
   ['http://example/a/b#c/../d', 'foo:bar', 'http://example/a/b#c/../d'],

    // 82-88
   ['http:this', 'http://example.org/base/uri', 'http:this'],
   ['http:this', 'http:base', 'http:this'],
   ['.//g', 'f:/a', 'f://g'],
   ['b/c//d/e', 'f://example.org/base/a', 'f://example.org/base/b/c//d/e'],
   ['m2@example.ord/c2@example.org', 'mid:m@example.ord/c@example.org', 'mid:m@example.ord/m2@example.ord/c2@example.org'],
   ['mini1.xml', 'file:///C:/DEV/Haskell/lib/HXmlToolbox-3.01/examples/', 'file:///C:/DEV/Haskell/lib/HXmlToolbox-3.01/examples/mini1.xml'],
   ['../b/c', 'foo:a/y/z', 'foo:a/b/c']

];

var relativeTests = [
   ['s://ex/a/b/c', 's://ex/a/d', 'b/c', 'b/c'],
   ['s://ex/b/b/c', 's://ex/a/d', '/b/b/c', undefined],
   ['s://ex/a/b/c', 's://ex/a/b/', 'c', 'c'],
   ['s://ex/a/b/c', 's://ex/b/../a/', 'b/c', 'b/c'],
   ['s://other.ex/a/b/c', 's://ex/a/d', undefined, undefined],
   ['s://ex/a/b/c', 's://other.ex/a/d', undefined, undefined],
   ['t://ex/a/b/c', 's://ex/a/d', undefined, undefined],
   ['s://ex/a/b/c', 't://ex/a/d', undefined, undefined],
   ['s://ex/a', 's://ex/b/c/d', '/a', undefined],
   ['s://ex/b/c/d', 's://ex/a', 'b/c/d', 'b/c/d'],
   ['s://ex/a/b/c?h', 's://ex/a/d?w', 'b/c?h', 'b/c?h'],
   ['s://ex/a/b/c#h', 's://ex/a/d#w', 'b/c#h', 'b/c#h'],
   ['s://ex/a/b/c?h#i', 's://ex/a/d?w#j', 'b/c?h#i', 'b/c?h#i'],
   ['s://ex/a#i', 's://ex/a', '#i', '#i'],
   ['s://ex/a?i', 's://ex/a', '?i', '?i'],

    // This is the kind of case which might indicate that we ought to always
    // return strings, and that returning `undefined` is dangerous.
   ['s://ex/a/b/', 's://ex/a/b/', '', ''],
   ['s://ex/a/b', 's://ex/a/b', '', ''],
   ['s://ex/', 's://ex/', '', ''],

    //('foo', 'bar', undefined, undefined],

    // This is the kind of case where we might want to instead return 'b/c'
    //('b/c', 's://ex/a/d', undefined, undefined],

    // Some tests specific to when we use isSubPath = False:
   ['s://ex/a/b/c', 's://ex/a/d/c', '../b/c', undefined],
   ['s://ex/a/b/c/', 's://ex/a/d/c', '../b/c/', undefined],
   ['s://ex/a/b/c/d', 's://ex/a/d/c/d', '../../b/c/d', undefined],
   ['s://ex/a/b/c', 's://ex/d/e/f', '/a/b/c', undefined],
   ['s://ex/a/b/', 's://ex/a/c/d/e', '../../b/', undefined],

    // Some tests to ensure that empty path segments don't cause problems.
   ['s://ex/a/b', 's://ex/a//b/c', '../../b', undefined],
   ['s://ex/a///b', 's://ex/a/', './//b', './//b'],
   ['s://ex/a/', 's://ex/a///b', '../../', undefined],
   ['s://ex/a//b/c', 's://ex/a/b', './/b/c', './/b/c']
];

info('resolve');
forEach(resolveTests, function (test) {
    args(test, function (rel, base, abs) {
        if (!assertEval(sprintf(
            "resolve(%s, %s) == %s",
            repr(rel),
            repr(base),
            repr(abs)
        )))
            log(repr(resolve(rel, base)));
    });
});

info('relative');
forEach(relativeTests, function (test) {
    args(test, function (abs, base, rel) {
        if (!assertEval(sprintf(
            "relative(%s, %s) == %s",
            repr(abs),
            repr(base),
            repr(rel)
        )))
            log(repr(relative(abs, base)));
    });
});

/*license

    Legal
    =======
    
    Chiron is a component of the Tale web-game project.
    
    See <credit.txt> for a complete list of
    contributions and their licenses.  All contributions are provided
    under permissive, non-viral licenses including MIT, BSD, Creative Commons
    Attribution 2.5, Public Domain, or Unrestricted.
    
    
    License
    =======
    
    Copyright (c) 2002-2008 Kris Kowal <http://cixar.com/~kris.kowal>
    MIT License
    
    
    MIT License
    -----------
    
    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:
    
    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.

*/

